package com.vf.admin.common;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 拦截过滤注册
 */
@Component
public class RegistInterceptor implements WebMvcConfigurer {
    @Autowired
    PerMissInterceptor perMissInterceptor;
    @Autowired
    TokenInterceptor tokenInterceptor;

    /**
     * 添加静态资源文件，外部可以直接访问地址
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //registry.addResourceHandler("/templates/**").addResourceLocations("classpath:/templates/");
        registry.addResourceHandler("/liaostatics/**").addResourceLocations("file:D:\\ziyuan\\liaostatics\\");
//        registry.addResourceHandler("/html/**").addResourceLocations("file:D:\\zyyziyuan\\html\\");
//        registry.addResourceHandler("/reg/**").addResourceLocations("file:D:\\zyyziyuan\\reg\\");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(perMissInterceptor);
        //主要给手机端使用
        List<String> patterns = new ArrayList<>();
        patterns.add("/app/login/login");
        patterns.add("/app/zhuce/sendCode");
        patterns.add("/api/white/reqAddress");
        patterns.add("/api/ipGetAddress/getAddress");
        patterns.add("/api/ossSts/callBackFiInfo");
        patterns.add("/api/ip/isWhiteIp");
        patterns.add("/api/sandPay/wxCallBack");
        patterns.add("/api/sandPay/zfbCallBack");
        patterns.add("/api/aliPayVersion3/fei/notifyUrl");
        patterns.add("/api/aliPayVersion3/fei/notifyUrl1");
        patterns.add("/api/aliPayVersion3/fei/notifyUrl2");
        patterns.add("/api/aliPayVersion3/fei/notifyUrl3");
        patterns.add("/api/aliPayVersion3/fei/notifyUrl4");
        registry.addInterceptor(tokenInterceptor)
                .addPathPatterns("/**")
                //过滤掉不需要token的接口
                .excludePathPatterns(patterns);
    }

    /**
     * 把springboot默认的json替换为阿里巴巴的fastjson
     * @param converters
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    /*converters = converters.stream()
            .filter((converter)-> !(converter instanceof MappingJackson2HttpMessageConverter))
            .collect(Collectors.toList());
    for (HttpMessageConverter<?> converter : converters) {
        if (converter instanceof MappingJackson2HttpMessageConverter){
            converters.remove(converter);
        }
    }*/
        Iterator<HttpMessageConverter<?>> iterator = converters.iterator();
        while(iterator.hasNext()){
            HttpMessageConverter<?> converter = iterator.next();
            if(converter instanceof MappingJackson2HttpMessageConverter){
                iterator.remove();
            }
        }
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();

        //升级最新版本需加=============================================================
        List<MediaType> supportedMediaTypes = new ArrayList<>();
        supportedMediaTypes.add(MediaType.APPLICATION_JSON);
        supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        supportedMediaTypes.add(MediaType.APPLICATION_ATOM_XML);
        supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
        supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
        supportedMediaTypes.add(MediaType.APPLICATION_PDF);
        supportedMediaTypes.add(MediaType.APPLICATION_RSS_XML);
        supportedMediaTypes.add(MediaType.APPLICATION_XHTML_XML);
        supportedMediaTypes.add(MediaType.APPLICATION_XML);
        supportedMediaTypes.add(MediaType.IMAGE_GIF);
        supportedMediaTypes.add(MediaType.IMAGE_JPEG);
        supportedMediaTypes.add(MediaType.IMAGE_PNG);
        supportedMediaTypes.add(MediaType.TEXT_EVENT_STREAM);
        supportedMediaTypes.add(MediaType.TEXT_HTML);
        supportedMediaTypes.add(MediaType.TEXT_MARKDOWN);
        supportedMediaTypes.add(MediaType.TEXT_PLAIN);
        supportedMediaTypes.add(MediaType.TEXT_XML);
        fastJsonHttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes);


        //自定义配置...
        FastJsonConfig config = new FastJsonConfig();
        config.setSerializerFeatures(SerializerFeature.QuoteFieldNames,
                SerializerFeature.WriteEnumUsingToString,
                /*SerializerFeature.WriteMapNullValue,*/
                SerializerFeature.WriteDateUseDateFormat,
                SerializerFeature.DisableCircularReferenceDetect);
        fastJsonHttpMessageConverter.setFastJsonConfig(config);
        converters.add(fastJsonHttpMessageConverter);
    }
}
